<template>
  <div class="app-container" v-if="show">
    <el-form
      class="ry_form"
      v-show="showSearch"
      :inline="true"
      label-width="100px"
      :model="queryParams"
      ref="queryForm"
      size="medium"
    >
      <!-- <el-form-item label="类型" prop="panelType">
        <DictRadio
          v-model="queryParams.panelType"
          :radioData="dict.type.wms_inventory_panel_type"
          size="small"
          @change="handleQuery"
        ></DictRadio>
      </el-form-item> -->
      <el-form-item label="物料" prop="itemId">
        <item-select v-model="queryParams.itemId"></item-select>
      </el-form-item>
      <el-form-item label="仓库" prop="rackId">
        <wms-warehouse-cascader
          v-model="queryParams.place"
        ></wms-warehouse-cascader>
      </el-form-item>
      <el-form-item label="库存" prop="quantity">
        <number-range v-model="queryParams.quantityRange"></number-range>
      </el-form-item>
      <el-form-item class="flex_one tr">
        <el-button
          icon="el-icon-search"
          size="mini"
          type="primary"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row class="mb8" :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['wms:item:export']"
          >导出
        </el-button>
      </el-col>
      <right-toolbar
        :columns="columns"
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <component
      :is="currentComponent"
      v-loading="loading"
      @update="getList"
      :table-data="wmsInventoryList"
    ></component>
    <pagination
      v-show="total&gt;0"
      :limit.sync="pageReq.size"
      :page.sync="pageReq.page"
      :total="total"
      @pagination="getList"
    ></pagination>
  </div>
</template>

<script>
// import { listWmsInventory, exportWmsInventory } from "@/api/wms/inventory";
// import NumberRange from "@/components/NumberRange";
// import ItemSelect from "@/components/ItemSelect";
import PanelByItem from "@/views/example/inventory/component/PanelByItem.vue";
import PanelByItemType from "@/views/example/inventory/component/PanelByItemType.vue";
import PanelByWarehouse from "@/views/example/inventory/component/PanelByWarehouse.vue";
import PanelByArea from "@/views/example/inventory/component/PanelByArea.vue";
// import { isStarRepo } from "@/utils/is-star-plugin";
// import { mapGetters } from "vuex";
import { listBrandChannelShop, listAll } from "@/api/chart/brandChannelShop";
export default {
  name: "WmsInventory",
  components: {
    PanelByArea,
    PanelByWarehouse,
    PanelByItemType,
    PanelByItem,
    // ItemSelect,
    // NumberRange,
  },
  // dicts: ["wms_inventory_panel_type"],
  data() {
    return {
      show: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存表格数据
      wmsInventoryList: [],
      // 查询参数
      queryParams: {
        itemId: null,
        place: null,
        quantityRange: [],
        panelType: 5,
      },
      pageReq: {
        page: 1,
        size: 10,
      },
      // 表单参数
      form: {},
      columns: [],
      panelType: 5,
    };
  },
  created() {
    // const res = await isStarRepo(
    //   "zccbbg",
    //   "RuoYi-Mall",
    //   this.userId,
    //   "http://wms.ichengle.top/inventory",
    //   "ruoyi-mall-商城",
    //   "https://gitee.com/zccbbg/RuoYi-Mall"
    // );
    // this.show = res;
    // if (res) {
    //   this.getList();
    // }
    this.getList();
  },
  computed: {
    // ...mapGetters(["userId"]),
    currentComponent() {
      let type = parseInt(this.panelType);
      switch (type) {
        case 5:
          return "panel-by-warehouse";
        case 10:
          return "panel-by-area";
        case 20:
          return "panel-by-item-type";
        default:
          return "";
      }
    },
  },
  methods: {
    /** 查询库存列表 */
    getList() {
      this.loading = true;
      const { place, quantityRange, itemId, panelType } = this.queryParams;
      this.panelType = panelType;
      const [warehouseId, areaId, rackId] = place || [];
      const [quantityStart, quantityEnd] = quantityRange || [];
      const query = {
        warehouseId,
        areaId,
        rackId,
        itemId,
        quantityStart,
        quantityEnd,
        panelType,
      };
      const pageReq = { ...this.pageReq };
      pageReq.page -= 1;
      // listWmsInventory(query, pageReq).then((response) => {
      listAll().then((response) => {
        response = {
          content: [
            {
              createBy: null,
              createTime: null,
              updateBy: null,
              updateTime: null,
              id: null,
              itemId: 943,
              itemNo: "788888",
              itemName: "567",
              itemTypeName: "杯具",
              rackId: null,
              rackName: null,
              warehouseId: null,
              warehouseName: null,
              areaId: null,
              areaName: null,
              quantity: 4.0,
              remark: null,
              saftyQuantity: null,
              itemDelFlag: 1,
              warehouseDelFlag: null,
            },
            {
              createBy: null,
              createTime: null,
              updateBy: null,
              updateTime: null,
              id: null,
              itemId: 939,
              itemNo: "a99",
              itemName: "2月茶杯",
              itemTypeName: "杯具",
              rackId: null,
              rackName: null,
              warehouseId: 1033,
              warehouseName: "小二楼仓库",
              areaId: null,
              areaName: null,
              quantity: 1.0,
              remark: null,
              saftyQuantity: null,
              itemDelFlag: 1,
              warehouseDelFlag: 0,
            },
            {
              createBy: null,
              createTime: null,
              updateBy: null,
              updateTime: null,
              id: null,
              itemId: 944,
              itemNo: "KD001S码",
              itemName: "连衣裙",
              itemTypeName: null,
              rackId: null,
              rackName: null,
              warehouseId: 1033,
              warehouseName: "小二楼仓库",
              areaId: null,
              areaName: null,
              quantity: 99.0,
              remark: null,
              saftyQuantity: null,
              itemDelFlag: 0,
              warehouseDelFlag: 0,
            },
            {
              createBy: null,
              createTime: null,
              updateBy: null,
              updateTime: null,
              id: null,
              itemId: 945,
              itemNo: "001",
              itemName: "001",
              itemTypeName: "iPhone15",
              rackId: null,
              rackName: null,
              warehouseId: 1033,
              warehouseName: "小二楼仓库",
              areaId: null,
              areaName: null,
              quantity: 105.0,
              remark: null,
              saftyQuantity: null,
              itemDelFlag: 0,
              warehouseDelFlag: 0,
            },
            {
              createBy: null,
              createTime: null,
              updateBy: null,
              updateTime: null,
              id: null,
              itemId: 946,
              itemNo: "A0001",
              itemName: "232",
              itemTypeName: null,
              rackId: null,
              rackName: null,
              warehouseId: 1033,
              warehouseName: "小二楼仓库",
              areaId: null,
              areaName: null,
              quantity: 3.0,
              remark: null,
              saftyQuantity: null,
              itemDelFlag: 0,
              warehouseDelFlag: 0,
            },
            {
              createBy: null,
              createTime: null,
              updateBy: null,
              updateTime: null,
              id: null,
              itemId: 948,
              itemNo: "1",
              itemName: "半成品",
              itemTypeName: "半成品",
              rackId: null,
              rackName: null,
              warehouseId: 1033,
              warehouseName: "小二楼仓库",
              areaId: null,
              areaName: null,
              quantity: 3.0,
              remark: null,
              saftyQuantity: null,
              itemDelFlag: 0,
              warehouseDelFlag: 0,
            },
            {
              createBy: null,
              createTime: null,
              updateBy: null,
              updateTime: null,
              id: null,
              itemId: 950,
              itemNo: "001",
              itemName: "电脑",
              itemTypeName: "电子设备",
              rackId: null,
              rackName: null,
              warehouseId: 1033,
              warehouseName: "小二楼仓库",
              areaId: null,
              areaName: null,
              quantity: 0.0,
              remark: null,
              saftyQuantity: null,
              itemDelFlag: 0,
              warehouseDelFlag: 0,
            },
            {
              createBy: null,
              createTime: null,
              updateBy: null,
              updateTime: null,
              id: null,
              itemId: 941,
              itemNo: "t001",
              itemName: "测试物料",
              itemTypeName: null,
              rackId: null,
              rackName: null,
              warehouseId: 1034,
              warehouseName: "2号货架",
              areaId: null,
              areaName: null,
              quantity: 111.0,
              remark: null,
              saftyQuantity: null,
              itemDelFlag: 0,
              warehouseDelFlag: 0,
            },
            {
              createBy: null,
              createTime: null,
              updateBy: null,
              updateTime: null,
              id: null,
              itemId: 943,
              itemNo: "788888",
              itemName: "567",
              itemTypeName: "杯具",
              rackId: null,
              rackName: null,
              warehouseId: 1034,
              warehouseName: "2号货架",
              areaId: null,
              areaName: null,
              quantity: 1.0,
              remark: null,
              saftyQuantity: null,
              itemDelFlag: 1,
              warehouseDelFlag: 0,
            },
            {
              createBy: null,
              createTime: null,
              updateBy: null,
              updateTime: null,
              id: null,
              itemId: 944,
              itemNo: "KD001S码",
              itemName: "连衣裙",
              itemTypeName: null,
              rackId: null,
              rackName: null,
              warehouseId: 1034,
              warehouseName: "2号货架",
              areaId: null,
              areaName: null,
              quantity: 5.0,
              remark: null,
              saftyQuantity: null,
              itemDelFlag: 0,
              warehouseDelFlag: 0,
            },
          ],
          pageable: {
            sort: {
              sorted: false,
              unsorted: true,
              empty: true,
            },
            pageNumber: 0,
            pageSize: 10,
            offset: 0,
            paged: true,
            unpaged: false,
          },
          last: false,
          totalElements: 17,
          totalPages: 2,
          first: true,
          sort: {
            sorted: false,
            unsorted: true,
            empty: true,
          },
          numberOfElements: 10,
          size: 10,
          number: 0,
          empty: false,
        };
        const { content, totalElements } = response;
        this.wmsInventoryList = content;

        if (panelType == 5 || panelType == 20) {
          // 10 库区需要考虑库区是否为空
          content.forEach((item) => {
            if (!item.warehouseName) {
              item.warehouseName = "暂未分配仓库";
            }
            if (item.areaName) {
              item.warehouseName = item.warehouseName + "/" + item.areaName;
            }
          });
        } else if (panelType == 10) {
          // 库区
          content.forEach((item) => {
            if (!item.warehouseName) {
              item.warehouseName = "暂未分配仓库";
            }
            if (!item.areaName) {
              item.areaName = "暂未分配库区";
            }
          });
        }
        this.total = totalElements;
        this.loading = false;
      });
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        itemId: null,
        rackId: null,
        quantity: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.pageReq.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有库存数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportWmsInventory(queryParams);
        })
        .then((response) => {
          this.$download.download(response);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

